using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Data.SqlClient;
//using HIS211201130薛星宇.




namespace HIS211201130薛星宇
{
    public partial class Form1 : Form
    {

        private int maxInpute=0;
        private String username;
        private String pwd;

        public Form1()
        {
            InitializeComponent();
        }

        private bool validateInout() 
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("用户名不能为空", "登录提示！", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBox1.Focus();
                maxInpute++;

                return false;
            }
            else if (textBox2.Text == "")
            {
                MessageBox.Show("密码不能为空", "登录提示！", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBox2.Focus();
                maxInpute++;
                return false;
            }
            else
            {
                return true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //在工具类中获取数据库连接
            SqlConnection coon= myDbClass.coon;

            //打开数据库连接。
            myDbClass.opendatabase();

            MessageBox.Show("数据库连接ok！！！");


            //获取输入框数据
            username = textBox1.Text.Trim();
            pwd = textBox2.Text.Trim();

            //调用参数校验函数
            bool result = validateInout();

            //参数无误再执行sql语句
            if (result == true)
            { 
                //定义sql命令
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "select count(*) from 员工档案 where 员工编号 = '" + username + "' and 登录口令='" + pwd + "' ";
                cmd.Connection = coon;

                int n = Convert.ToInt32(cmd.ExecuteScalar());
                if (n >= 1)
                {
                    MessageBox.Show("登录成功！！！");
                    if (maxInpute = 3)
                        {
                            MessageBox.Show("您错误次数太多。请重来！");
                                Application.Exit();
                        }

                    main frm = new main();
                    frm.Show();
                
                }else
                {
                    maxInpute++;
                    MessageBox.Show("账号或密码有误！");
                }

            }


            //关闭连接
            coon.Close();


        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}
